﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Entities.Addresses
{
    public partial class AddressExtractorEditor : Form, IComponentEditor 
    {
        private object _ActivityNode;
        private AddressExtractorComponent _AddrExComponent;


        public AddressExtractorEditor()
        {
            InitializeComponent();
        }

        #region IComponentEditor Members

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof (AddressExtractorComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._AddrExComponent = (AddressExtractorComponent)wfComponent;

            this.cbo_SrcDbProvider.SelectedIndex = (int)this._AddrExComponent.DbProvider;
            this.txt_SrcConnStr.Text = this._AddrExComponent.ConnStr;
            this.rt_SrcSql.Text = this._AddrExComponent.SelectSql;
            this.cbo_SrcKey.Items.Clear();
            this.cbo_AddrField.Items.Clear();
            if(!string.IsNullOrEmpty(this.txt_SrcConnStr.Text) && !string.IsNullOrEmpty(this.rt_SrcSql.Text))
            {
                TableColumn[] srcCols =
                    DbUtil.GetTableColumns(this._AddrExComponent.DbProvider,
                                           this.txt_SrcConnStr.Text, 
                                           this.rt_SrcSql.Text);
                if(srcCols !=null && srcCols.Length>0)
                {
                    foreach(TableColumn col in srcCols)
                    {
                        this.cbo_SrcKey.Items.Add(col.ColumnName);
                        this.cbo_AddrField.Items.Add(col.ColumnName);
                    }

                    if(!string.IsNullOrEmpty(this._AddrExComponent.PKField))
                    {
                        for(int i=0;i<this.cbo_SrcKey.Items.Count;i++)
                        {
                            if(this.cbo_SrcKey.Items[i].ToString()==this._AddrExComponent.PKField)
                            {
                                this.cbo_SrcKey.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if(!string.IsNullOrEmpty(this._AddrExComponent.FullAddressField))
                    {
                        for(int i=0;i<this.cbo_AddrField.Items.Count;i++)
                        {
                            if(this.cbo_AddrField.Items[i].ToString()==this._AddrExComponent.FullAddressField)
                            {
                                this.cbo_AddrField.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }
            }

            this.cbo_TgtDbProvider.SelectedIndex = (int)this._AddrExComponent.TgtDbProvider;
            this.txt_TgtConnStr.Text = this._AddrExComponent.TgtConnStr;
            this.cbo_TgtTable.Items.Clear();
            if(!string.IsNullOrEmpty(this._AddrExComponent.TgtConnStr))
            {
                string[] tblNames =
                    DbUtil.GetTableNames(
                        this._AddrExComponent.TgtDbProvider, string.Empty, this._AddrExComponent.TgtConnStr);
                if(tblNames !=null && tblNames.Length>0)
                {
                    foreach(string tblName in tblNames)
                    {
                        this.cbo_TgtTable.Items.Add(tblName);
                    }

                    if(!string.IsNullOrEmpty(this._AddrExComponent.TgtTable))
                    {
                        for(int i=0;i<this.cbo_TgtTable.Items.Count;i++)
                        {
                            if(this.cbo_TgtTable.Items[i].ToString()==this._AddrExComponent.TgtTable)
                            {
                                this.cbo_TgtTable.SelectedIndex = i;
                                break;
                            }
                        }
                    }

                    // tgt field
                    if(this.cbo_AddrID.Items.Count>0 && !string.IsNullOrEmpty(this._AddrExComponent.AddrIDField))
                    {
                        for(int i=0;i<this.cbo_AddrID.Items.Count;i++)
                        {
                            if(this.cbo_AddrID.Items[i].ToString()==this._AddrExComponent.AddrIDField)
                            {
                                this.cbo_AddrID.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_RefKey.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.RefKeyField))
                    {
                        for (int i = 0; i < this.cbo_RefKey.Items.Count; i++)
                        {
                            if (this.cbo_RefKey.Items[i].ToString() == this._AddrExComponent.RefKeyField)
                            {
                                this.cbo_RefKey.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_SubDiv.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.SubDivField))
                    {
                        for (int i = 0; i < this.cbo_SubDiv.Items.Count; i++)
                        {
                            if (this.cbo_SubDiv.Items[i].ToString() == this._AddrExComponent.SubDivField)
                            {
                                this.cbo_SubDiv.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_Org.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.OrgField))
                    {
                        for (int i = 0; i < this.cbo_Org.Items.Count; i++)
                        {
                            if (this.cbo_Org.Items[i].ToString() == this._AddrExComponent.OrgField)
                            {
                                this.cbo_Org.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_Street.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.StreetField))
                    {
                        for (int i = 0; i < this.cbo_Street.Items.Count; i++)
                        {
                            if (this.cbo_Street.Items[i].ToString() == this._AddrExComponent.StreetField)
                            {
                                this.cbo_Street.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_City.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.CityField))
                    {
                        for (int i = 0; i < this.cbo_City.Items.Count; i++)
                        {
                            if (this.cbo_City.Items[i].ToString() == this._AddrExComponent.CityField)
                            {
                                this.cbo_City.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_Region.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.RegionField))
                    {
                        for (int i = 0; i < this.cbo_Region.Items.Count; i++)
                        {
                            if (this.cbo_Region.Items[i].ToString() == this._AddrExComponent.RegionField)
                            {
                                this.cbo_Region.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_PostalCode.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.PostalCodeField))
                    {
                        for (int i = 0; i < this.cbo_PostalCode.Items.Count; i++)
                        {
                            if (this.cbo_PostalCode.Items[i].ToString() == this._AddrExComponent.PostalCodeField)
                            {
                                this.cbo_PostalCode.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_Country.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.CountryField))
                    {
                        for (int i = 0; i < this.cbo_Country.Items.Count; i++)
                        {
                            if (this.cbo_Country.Items[i].ToString() == this._AddrExComponent.CountryField)
                            {
                                this.cbo_Country.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }
            }
        }

        public void SaveSettings()
        {
            this._AddrExComponent.DbProvider = (DataProviderType)this.cbo_SrcDbProvider.SelectedIndex;
            this._AddrExComponent.ConnStr = this.txt_SrcConnStr.Text;
            this._AddrExComponent.SelectSql = this.rt_SrcSql.Text;
            this._AddrExComponent.PKField = this.cbo_SrcKey.Text;
            this._AddrExComponent.FullAddressField = this.cbo_AddrField.Text;

            this._AddrExComponent.TgtDbProvider = (DataProviderType)this.cbo_TgtDbProvider.SelectedIndex;
            this._AddrExComponent.TgtConnStr = this.txt_TgtConnStr.Text;
            this._AddrExComponent.TgtTable = this.cbo_TgtTable.Text;
            this._AddrExComponent.AddrIDField = this.cbo_AddrID.Text;
            this._AddrExComponent.RefKeyField = this.cbo_RefKey.Text;

            this._AddrExComponent.SubDivField = this.cbo_SubDiv.Text;
            this._AddrExComponent.OrgField = this.cbo_Org.Text;
            this._AddrExComponent.StreetField = this.cbo_Street.Text;
            this._AddrExComponent.CityField = this.cbo_City.Text;
            this._AddrExComponent.RegionField = this.cbo_Region.Text;
            this._AddrExComponent.PostalCodeField = this.cbo_PostalCode.Text;
            this._AddrExComponent.CountryField = this.cbo_Country.Text;
        }

        public TestResult Test()
        {
            throw new NotImplementedException();
        }

        #endregion

        private void btn_RefreshSrc_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txt_SrcConnStr.Text) && 
                !string.IsNullOrEmpty(this.rt_SrcSql.Text))
            {
                this.cbo_SrcKey.Items.Clear();
                this.cbo_AddrField.Items.Clear();

                TableColumn[] srcCols =
                    DbUtil.GetTableColumns(this._AddrExComponent.DbProvider,
                                           this.txt_SrcConnStr.Text,
                                           this.rt_SrcSql.Text);
                if (srcCols != null && srcCols.Length > 0)
                {
                    foreach (TableColumn col in srcCols)
                    {
                        this.cbo_SrcKey.Items.Add(col.ColumnName);
                        this.cbo_AddrField.Items.Add(col.ColumnName);
                    }

                    if (!string.IsNullOrEmpty(this._AddrExComponent.PKField))
                    {
                        for (int i = 0; i < this.cbo_SrcKey.Items.Count; i++)
                        {
                            if (this.cbo_SrcKey.Items[i].ToString() == this._AddrExComponent.PKField)
                            {
                                this.cbo_SrcKey.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(this._AddrExComponent.FullAddressField))
                    {
                        for (int i = 0; i < this.cbo_AddrField.Items.Count; i++)
                        {
                            if (this.cbo_AddrField.Items[i].ToString() == this._AddrExComponent.FullAddressField)
                            {
                                this.cbo_AddrField.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }
            }

        }

        private void cbo_TgtTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_TgtTable.SelectedIndex>=0)
            {
                DataProviderType dbProvider = (DataProviderType)this.cbo_TgtDbProvider.SelectedIndex;
                string connStr = this.txt_TgtConnStr.Text;
                string tblName = this.cbo_TgtTable.SelectedItem.ToString();

                TableColumn[] cols =
                    DbUtil.GetTableColumns(dbProvider, connStr, string.Empty, tblName);
                this.cbo_AddrID.Items.Clear();
                this.cbo_RefKey.Items.Clear();
                this.cbo_SubDiv.Items.Clear();
                this.cbo_Org.Items.Clear();
                this.cbo_Street.Items.Clear();
                this.cbo_City.Items.Clear();
                this.cbo_Region.Items.Clear();
                this.cbo_PostalCode.Items.Clear();
                this.cbo_Country.Items.Clear();

                if(cols !=null && cols.Length>0)
                {
                    foreach(TableColumn col in cols)
                    {
                        this.cbo_AddrID.Items.Add(col.ColumnName);
                        this.cbo_RefKey.Items.Add(col.ColumnName);
                        this.cbo_SubDiv.Items.Add(col.ColumnName);
                        this.cbo_Org.Items.Add(col.ColumnName);
                        this.cbo_Street.Items.Add(col.ColumnName);
                        this.cbo_City.Items.Add(col.ColumnName);
                        this.cbo_Region.Items.Add(col.ColumnName);
                        this.cbo_PostalCode.Items.Add(col.ColumnName);
                        this.cbo_Country.Items.Add(col.ColumnName);
                    }
                }
            }
        }

        private void btn_RefreshTgtTable_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txt_TgtConnStr.Text))
            {
                DataProviderType dbProvider = (DataProviderType)this.cbo_TgtDbProvider.SelectedIndex;
                string connStr = this.txt_TgtConnStr.Text;
                string[] tblNames = DbUtil.GetTableNames(dbProvider, string.Empty, connStr);
                this.cbo_TgtTable.Items.Clear();

                if (tblNames != null && tblNames.Length > 0)
                {
                    foreach (string tblName in tblNames)
                    {
                        this.cbo_TgtTable.Items.Add(tblName);
                    }

                    if (!string.IsNullOrEmpty(this._AddrExComponent.TgtTable))
                    {
                        for (int i = 0; i < this.cbo_TgtTable.Items.Count; i++)
                        {
                            if (this.cbo_TgtTable.Items[i].ToString() == this._AddrExComponent.TgtTable)
                            {
                                this.cbo_TgtTable.SelectedIndex = i;
                                break;
                            }
                        }
                    }

                    // tgt field
                    if (this.cbo_AddrID.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.AddrIDField))
                    {
                        for (int i = 0; i < this.cbo_AddrID.Items.Count; i++)
                        {
                            if (this.cbo_AddrID.Items[i].ToString() == this._AddrExComponent.AddrIDField)
                            {
                                this.cbo_AddrID.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_RefKey.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.RefKeyField))
                    {
                        for (int i = 0; i < this.cbo_RefKey.Items.Count; i++)
                        {
                            if (this.cbo_RefKey.Items[i].ToString() == this._AddrExComponent.RefKeyField)
                            {
                                this.cbo_RefKey.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_SubDiv.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.SubDivField))
                    {
                        for (int i = 0; i < this.cbo_SubDiv.Items.Count; i++)
                        {
                            if (this.cbo_SubDiv.Items[i].ToString() == this._AddrExComponent.SubDivField)
                            {
                                this.cbo_SubDiv.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_Org.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.OrgField))
                    {
                        for (int i = 0; i < this.cbo_Org.Items.Count; i++)
                        {
                            if (this.cbo_Org.Items[i].ToString() == this._AddrExComponent.OrgField)
                            {
                                this.cbo_Org.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_Street.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.StreetField))
                    {
                        for (int i = 0; i < this.cbo_Street.Items.Count; i++)
                        {
                            if (this.cbo_Street.Items[i].ToString() == this._AddrExComponent.StreetField)
                            {
                                this.cbo_Street.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_City.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.CityField))
                    {
                        for (int i = 0; i < this.cbo_City.Items.Count; i++)
                        {
                            if (this.cbo_City.Items[i].ToString() == this._AddrExComponent.CityField)
                            {
                                this.cbo_City.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_Region.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.RegionField))
                    {
                        for (int i = 0; i < this.cbo_Region.Items.Count; i++)
                        {
                            if (this.cbo_Region.Items[i].ToString() == this._AddrExComponent.RegionField)
                            {
                                this.cbo_Region.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_PostalCode.Items.Count > 0 &&
                        !string.IsNullOrEmpty(this._AddrExComponent.PostalCodeField))
                    {
                        for (int i = 0; i < this.cbo_PostalCode.Items.Count; i++)
                        {
                            if (this.cbo_PostalCode.Items[i].ToString() == this._AddrExComponent.PostalCodeField)
                            {
                                this.cbo_PostalCode.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                    if (this.cbo_Country.Items.Count > 0 && !string.IsNullOrEmpty(this._AddrExComponent.CountryField))
                    {
                        for (int i = 0; i < this.cbo_Country.Items.Count; i++)
                        {
                            if (this.cbo_Country.Items[i].ToString() == this._AddrExComponent.CountryField)
                            {
                                this.cbo_Country.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }
            }
        }

        private void btn_Test_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.rt_FullAddr.Text))
            {
                AddressParser parser = new AddressParser();
                Address addr = parser.ParseAddress(0, this.rt_FullAddr.Text);

                if (addr.SubDivision != null)
                    this.txt_SubDiv.Text = addr.SubDivision;
                else
                    this.txt_SubDiv.Text = string.Empty;
                if (addr.Organization != null)
                    this.txt_Org.Text = addr.Organization;
                else
                    this.txt_Org.Text = string.Empty;
                if (addr.Street != null)
                    this.txt_Street.Text = addr.Street;
                else
                    this.txt_Street.Text = string.Empty;
                if (addr.City != null)
                    this.txt_City.Text = addr.City;
                else
                    this.txt_City.Text = string.Empty;
                if (addr.Region != null)
                    this.txt_Region.Text = addr.Region;
                else
                    this.txt_Region.Text = string.Empty;
                if (addr.PostalCode != null)
                    this.txt_PostalCode.Text = addr.PostalCode;
                else
                    this.txt_PostalCode.Text = string.Empty;
                if (addr.Country != null)
                    this.txt_Country.Text = addr.Country;
                else
                    this.txt_Country.Text = string.Empty;
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();

            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._ActivityNode, this._AddrExComponent);

            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
